package com.dexgdx.game.structure.base;

import com.dexgdx.game.Game;
import com.dexgdx.game.constants.EMoveDirection;
import com.dexgdx.game.manager.AnimationManager;
import com.dexgdx.game.util.ObjectIdentification;

public class MobileObject extends BaseObject {

	private static final long serialVersionUID = -4802323628456349868L;

	protected boolean moving;
	protected BaseRegion moveTarget;
	protected float movementSpeed;
	
	protected EMoveDirection eMoveDirection;

	public MobileObject(float x, float y, float height, float width, float movementSpeed, 
			boolean solidObject, boolean animated, ObjectIdentification objectId) {
		super(x, y, height, width, solidObject, animated, objectId);
		this.movementSpeed = movementSpeed;
		this.eMoveDirection = EMoveDirection.EAST;
	}

	public BaseRegion getMoveTarget() {
		if(moveTarget == null)
			moveTarget = this;
		return moveTarget;
	}

	@Override
	public void loadImageTextureRegionAndFrame(int animIndex){
		imageTextureRegion = AnimationManager.getAnimationByKey(
				getPlayerNameId().getId(), 1, 1, getMoveDirection().ordinal()).getKeyFrame(Game.stateTime + animIndex, true);
		
		loadImageFrame();
	}
	
	public void loadImageTextureRegionAndStopFrame(){
		imageTextureRegion = AnimationManager.getAnimationByKey(
				getPlayerNameId().getId(), 1, 1, getMoveDirection().ordinal()).getStopFrame();
		
		loadImageFrame();
	}
	
	public void loadImageFrame(){
        float frameRatio = (float) imageTextureRegion.getRegionHeight() / (float)imageTextureRegion.getRegionWidth();
        if(imageBaseRegion == null){
        	imageBaseRegion = new BaseRegion(
        			x, 
        			y + width/2 - width * frameRatio, 
        			width, 
        			width * frameRatio);
        }
        else {
        	
        	imageBaseRegion.x = x;
        	imageBaseRegion.y = y + width/2 - width * frameRatio;
        	imageBaseRegion.width = width;
        	imageBaseRegion.height = width * frameRatio;
        }
	}
	
	public void startMoving(BaseRegion target){
		this.moveTarget = target;
		moving = true;
	}
	
	public void stopMoving(){
		this.moveTarget = null;
		moving = false;
	}

	public boolean isMoving() {
		return moving;
	}
	
	public boolean hasFinalizedMoving(){
		return true;
	}
	
	public float getMovementSpeed() {
		return movementSpeed;
	}

	public EMoveDirection getMoveDirection() {
		return eMoveDirection;
	}

	public void setMoveDirection(EMoveDirection eMoveDirection) {
		this.eMoveDirection = eMoveDirection;
	}
	
}
